package com.xy.modular.repair.controller;

import com.xy.modular.owner.service.IOwnerAuthLocalService;
import com.xy.modular.repair.model.Repair;
import com.xy.modular.repair.service.IRepairService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;

/**
 *
 * Created by BCK on 2019/09/14.
 */
@Controller
@RequestMapping("/wx/repair/")
public class RepairController extends BaseController {

	@Resource
	IRepairService repairService;
	@Resource
	IOwnerAuthLocalService ownerAuthLocalService;

	@SuppressWarnings("unused")
	private String dowImg(MultipartFile file) {
		File targetFile = null;
		String fileName = file.getOriginalFilename();// 获取文件名加后缀
		String filePath = "";
		if (fileName != null && fileName != "") {
			String path = "d:"; // 文件存储位置
			String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());// 文件后缀
			fileName = new Date().getTime() + "_" + new Random().nextInt(1000) + fileF;// 新的文件名

			// 先判断文件是否存在
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			String fileAdd = sdf.format(new Date());
			// 获取文件夹路径
			File file1 = new File(path + "/" + fileAdd);
			// 如果文件夹不存在则创建
			if (!file1.exists() && !file1.isDirectory()) {
				file1.mkdir();
			}
			// 将图片存入文件夹
			targetFile = new File(file1, fileName);
			filePath += fileAdd + "/" + fileName;
			try {
				// 将上传的文件写到服务器上指定的文件。
				file.transferTo(targetFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return filePath.substring(0, filePath.length() > 0 ? filePath.length() - 1 : 0);
	}

	@RequestMapping(value = "/save/file", method = RequestMethod.POST)
	@ResponseBody
	public String saveFile(Repair repair, @RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) {
		if (ownerAuthLocalService.getById(repair.getOwnerId()) != null) {
			Repair repair2 = repairService.getById(repair.getId());
			String filePath = dowImg(file);
			if (repair2 != null) {
				repair2.setImg(repair2.getImg() + ";" + filePath);
				repairService.update(repair2);
			} else {
				System.out.println(dowImg(file));
				repair.setId(UUIDUtils.UUID());
				repair.setImg(filePath);
				repairService.save(repair);
			}
			return repair.getId();
		}
		return "图片上传失败";
	}

	@RequestMapping("update")
	@ResponseBody
	public String update(Repair Repair) {
		repairService.update(Repair);
		return "";
	}

	@RequestMapping("list")
	@ResponseBody
	public MReturnObject list(HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addSortConditions("create_time", pageRequest.ORDER_DESC);
		return new MReturnObject(MReturnObject.SUCCESS, repairService.findAll(pageRequest));
	}
}
